home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_01 / cips3.c < prev    next >
C/C++ Source or Header  |  1992-04-08  |  15KB  |  513 lines

  1.  
  2.        /***********************************************
  3.        *
  4.        *       file d:\cips\addsub.c
  5.        *
  6.        *       Functions: This file contains
  7.        *          add_image_array
  8.        *          subtract_image_array
  9.        *
  10.        *       Purpose:
  11.        *          These functions implement
  12.        *          image addition and subtraction.
  13.        *
  14.        *       External Calls:
  15.        *          wtiff.c - does_not_exist
  16.        *                    round_off_image_size
  17.        *                    create_allocate_tiff_file
  18.        *                    write_array_into_tiff_image
  19.        *          tiff.c - read_tiff_header
  20.        *          rtiff.c - read_tiff_image
  21.        *
  22.        *
  23.        *       Modifications:
  24.        *          1 April 1992 - created
  25.        *
  26.        *************************************************/
  27.  
  28. #include "d:\cips\cips.h"
  29.  
  30.      /*******************************************
  31.      *
  32.      *   add_image_array(...
  33.      *
  34.      *   This function adds two ROWSxCOLS image
  35.      *   sections.  The image file named out_name
  36.      *   will receive the sum of the image file
  37.      *   named in1_name and the image file
  38.      *   named in2_name.
  39.      *
  40.      *******************************************/
  41.  
  42.  
  43. add_image_array(in1_name, in2_name, out_name, the_image, out_image,
  44.           il1, ie1, ll1, le1,
  45.           il2, ie2, ll2, le2,
  46.           il3, ie3, ll3, le3)
  47.    char   in1_name[], in2_name[], out_name[];
  48.    int    il1, ie1, ll1, le1,
  49.           il2, ie2, ll2, le2,
  50.           il3, ie3, ll3, le3;
  51.    short  the_image[ROWS][COLS],
  52.           out_image[ROWS][COLS];
  53.  
  54. {
  55.    int    i, j, length, max, width;
  56.    struct tiff_header_struct image_header;
  57.  
  58.  
  59.    if(does_not_exist(out_name)){
  60.       printf("\n\n output file does not exist %s", out_name);
  61.       read_tiff_header(in1_name, &image_header);
  62.       round_off_image_size(&image_header,
  63.                            &length, &width);
  64.       image_header.image_length = length*ROWS;
  65.       image_header.image_width  = width*COLS;
  66.       create_allocate_tiff_file(out_name, &image_header,
  67.                                 out_image);
  68.    }  /* ends if does_not_exist */
  69.  
  70.    read_tiff_header(in1_name, &image_header);
  71.  
  72.    max = 255;
  73.    if(image_header.bits_per_pixel == 4)
  74.       max = 16;
  75.  
  76.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  77.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  78.  
  79.    for(i=0; i<ROWS; i++){
  80.       for(j=0; j<COLS; j++){
  81.          out_image[i][j] = the_image[i][j] + out_image[i][j];
  82.          if(out_image[i][j] > max)
  83.             out_image[i][j] = max;
  84.       }  /* ends loop over j */
  85.    }  /* ends loop over i */
  86.  
  87.    write_array_into_tiff_image(out_name, out_image,
  88.                                il3, ie3, ll3, le3);
  89.  
  90. }  /* ends add_image_array */
  91.  
  92.  
  93.  
  94.  
  95.  
  96.      /*******************************************
  97.      *
  98.      *   subtract_image_array(...
  99.      *
  100.      *   This function subtracts two ROWSxCOLS image
  101.      *   sections.  The image file named out_name
  102.      *   will receive the difference of the image file
  103.      *   named in1_name and the image file
  104.      *   named in2_name.
  105.      *
  106.      *   out_name = in1_name - in2_name
  107.      *
  108.      *******************************************/
  109.  
  110.  
  111. subtract_image_array(in1_name, in2_name, out_name, the_image, out_image,
  112.           il1, ie1, ll1, le1,
  113.           il2, ie2, ll2, le2,
  114.           il3, ie3, ll3, le3)
  115.    char   in1_name[], in2_name[], out_name[];
  116.    int    il1, ie1, ll1, le1,
  117.           il2, ie2, ll2, le2,
  118.           il3, ie3, ll3, le3;
  119.    short  the_image[ROWS][COLS],
  120.           out_image[ROWS][COLS];
  121.  
  122. {
  123.    int    i, j, length, width;
  124.    struct tiff_header_struct image_header;
  125.  
  126.  
  127.    if(does_not_exist(out_name)){
  128.       printf("\n\n output file does not exist %s", out_name);
  129.       read_tiff_header(in1_name, &image_header);
  130.       round_off_image_size(&image_header,
  131.                            &length, &width);
  132.       image_header.image_length = length*ROWS;
  133.       image_header.image_width  = width*COLS;
  134.       create_allocate_tiff_file(out_name, &image_header,
  135.                                 out_image);
  136.    }  /* ends if does_not_exist */
  137.  
  138.    read_tiff_header(in1_name, &image_header);
  139.  
  140.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  141.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  142.  
  143.    for(i=0; i<ROWS; i++){
  144.       for(j=0; j<COLS; j++){
  145.          out_image[i][j] = the_image[i][j] - out_image[i][j];
  146.          if(out_image[i][j] < 0)
  147.             out_image[i][j] = 0;
  148.       }  /* ends loop over j */
  149.    }  /* ends loop over i */
  150.  
  151.    write_array_into_tiff_image(out_name, out_image,
  152.                                il3, ie3, ll3, le3);
  153.  
  154. }  /* ends subtract_image_array */
  155.  
  156.        /***********************************************
  157.        *
  158.        *       file d:\cips\cutp.c
  159.        *
  160.        *       Functions: This file contains
  161.        *          cut_image_piece
  162.        *          paste_image_piece
  163.        *          check_cut_and_paste_limits
  164.        *
  165.        *       Purpose:
  166.        *          These functions cut pieces out
  167.        *          of images and paste them back into
  168.        *          images.
  169.        *
  170.        *       External Calls:
  171.        *          wtiff.c - does_not_exist
  172.        *                    round_off_image_size
  173.        *                    create_allocate_tiff_file
  174.        *                    write_array_into_tiff_image
  175.        *          tiff.c - read_tiff_header
  176.        *          rtiff.c - read_tiff_image
  177.        *
  178.        *
  179.        *       Modifications:
  180.        *          3 April 1992 - created
  181.        *
  182.        *************************************************/
  183.  
  184.  
  185.      /*******************************************
  186.      *
  187.      *   cut_image_piece(...
  188.      *
  189.      *   This function cuts out a rectangular
  190.      *   piece of an image.  This rectangle can
  191.      *   be any shape so long as no dimension
  192.      *   is greater than ROWS or COLS.
  193.      *
  194.      *******************************************/
  195.  
  196.  
  197. cut_image_piece(name, the_image, il, ie, ll, le)
  198.    char   name[];
  199.    int    il, ie, ll, le;
  200.    short  the_image[ROWS][COLS];
  201.  
  202. {
  203.    if(does_not_exist(name)){
  204.       printf("\n\ncut_image_piece>> ERROR "
  205.              "image file does not exist %s", name);
  206.       return(-1);
  207.    }  /* ends if does_not_exist */
  208.  
  209.    read_tiff_image(name, the_image, il, ie, ll, le);
  210.  
  211. }  /* ends cut_image_piece */
  212.  
  213.  
  214.  
  215.  
  216.      /*******************************************
  217.      *
  218.      *   paste_image_piece(...
  219.      *
  220.      *   This function pastes a rectangular
  221.      *   piece of an image into another image.
  222.      *   This rectangle can be any shape so long
  223.      *   as no dimension is greater than ROWS or COLS.
  224.      *   The rectangle to be pasted into the image
  225.      *   is described by the il, ie, ll, le
  226.      *   parameters.
  227.      *
  228.      *   You pass is the out_image array just in
  229.      *   case you need to allocate the destination
  230.      *   image.
  231.      *
  232.      *******************************************/
  233.  
  234.  
  235. paste_image_piece(dest_name, source_name, the_image,
  236.                   out_image, il, ie, ll, le)
  237.    char   dest_name[], source_name[];
  238.    int    il, ie, ll, le;
  239.    short  the_image[ROWS][COLS],
  240.           out_image[ROWS][COLS];
  241.  
  242. {
  243.    struct tiff_header_struct image_header;
  244.  
  245.    if(does_not_exist(dest_name)){
  246.       printf("\n\ncut_image_piece>> "
  247.              "image file does not exist %s", dest_name);
  248.       read_tiff_header(source_name, &image_header);
  249.       create_allocate_tiff_file(dest_name, &image_header,
  250.                                 out_image);
  251.    }  /* ends if does_not_exist */
  252.  
  253.    write_array_into_tiff_image(dest_name, the_image,
  254.                                il, ie, ll, le);
  255.  
  256. }  /* ends paste_image_piece */
  257.  
  258.  
  259.  
  260.      /*******************************************
  261.      *
  262.      *   check_cut_and_paste_limits(...
  263.      *
  264.      *   This function looks at the line and
  265.      *   element parameters and ensures that they
  266.      *   are not bigger than ROWS and COLS.  If
  267.      *   they are bigger, the last element or
  268.